package com.atguigu.jxc.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.atguigu.jxc.dao.OverflowListGoodsDao;
import com.atguigu.jxc.dao.UserDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.DamageListGoods;
import com.atguigu.jxc.entity.OverflowList;
import com.atguigu.jxc.entity.OverflowListGoods;
import com.atguigu.jxc.entity.User;
import com.atguigu.jxc.service.GoodsService;
import com.atguigu.jxc.service.OverflowListGoodsService;
import com.atguigu.jxc.service.OverflowListService;
import com.google.gson.JsonArray;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 杨林
 * @create 2023-01-29 18:07 星期日
 * description:
 */
@Service
public class OverflowListGoodsServiceImpl implements OverflowListGoodsService {

    @Autowired
    OverflowListService overflowListService;

    @Autowired
    OverflowListGoodsDao overflowListGoodsDao;

    @Autowired
    UserDao userDao;

    @Autowired
    GoodsService goodsService;

    /**
     * 新增报溢单
     * @param overflowList
     * @param overflowListGoodsStr
     * @return
     */
    @Override
    public ServiceVO save(OverflowList overflowList, String overflowListGoodsStr) {
        //将json数组转换为实体类
        List<OverflowListGoods> overflowListGoodsList = JSONArray.parseArray(overflowListGoodsStr,OverflowListGoods.class);

        //获取当前用户id
        User currentUser = userDao.findUserByName((String) SecurityUtils.getSubject().getPrincipal());
        overflowList.setUserId(currentUser.getUserId());

        //保存overflowList
        overflowListService.save(overflowList);

        //获取overflowList的id
        Integer overflowListId = overflowList.getOverflowListId();

        for (OverflowListGoods overflowListGoods : overflowListGoodsList) {
            overflowListGoods.setOverflowListId(overflowListId);

            //商品数量溢出时，需要在库存中加上超出的数量
            Integer goodsNum = overflowListGoods.getGoodsNum();
            String goodsCode = overflowListGoods.getGoodsCode();

            boolean ifChange = goodsService.changeInventoryQuantity(goodsCode, goodsNum);
            if (ifChange == false){
                return new ServiceVO(ErrorCode.CHANGE_INVENTORY_QUANTITY_ERROR,ErrorCode.CHANGE_INVENTORY_QUANTITY_ERROR_MSG);
            }
        }

        //批量保存
        overflowListGoodsDao.bachSave(overflowListGoodsList);

        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }

    /**
     * 报溢单查询
     * @param sTime
     * @param eTime
     * @return
     */
    @Override
    public Map<String, Object> list(String sTime, String eTime) {
        Map<String,Object> map = new HashMap<>();

        List<OverflowList> overflowListList = overflowListService.list(sTime,eTime);

        map.put("rows",overflowListList);
        return map;
    }

    /**
     * 报溢单商品信息
     * @param overflowListId
     * @return
     */
    @Override
    public Map<String, Object> goodsList(Integer overflowListId) {
        Map<String,Object> map = new HashMap<>();
        List<OverflowListGoods> overflowListGoodsList = overflowListGoodsDao.goodsList(overflowListId);

        map.put("rows",overflowListGoodsList);
        return map;
    }
}
